/// <summary>
/// 作用是将.xlsx转换为.assets
/// </summary>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Codice.Client.BaseCommands;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using PlasticPipe.PlasticProtocol.Client.Proxies;
using UnityEditor;
using UnityEngine;

namespace ExcelSpace
{
    public class ExcelAssetPostprocessor : AssetPostprocessor
    {
        static string pyFile = Path.Combine(Application.dataPath, "Editor/Excel/Scripts/ExcelCreateCSData.py");
        static string csTemplate = Path.Combine(Application.dataPath + "/Editor/Excel/Scripts/ExcelCSTemplate.txt");
        static string excelClassTemplate = Path.Combine(Application.dataPath + "/Editor/Excel/Scripts/ExcelClassTemplate.txt");
        static string excelClassFile = Path.Combine(Application.dataPath + "/Scripts/Excel.cs");
        static string assetFolder = Path.Combine("Assets/Resources/Excel");

        static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            foreach (string asset in importedAssets) 
            {   
                if(asset.StartsWith("Assets/Editor/Excel/") && asset.EndsWith(".xlsx"))
                {
                    // 创世文件完成
                    SheetInfo info = new SheetInfo(asset);

                    // 创建 XXXSheet.cs
                    string argv = pyFile;
                    argv += " " + info.englishName;
                    argv += " " + info.filePath;
                    argv += " " + csTemplate;
                    argv += " " + info.csFilePath;
                    Tools.Run("py", argv);
                    AssetDatabase.Refresh();

                    // 创建资源
                    ExcelQuery query = new ExcelQuery(info);
                    query.CreateAsset(assetFolder);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();

                    Debug.Log($"重新加载了Excel文件 {info.fullName}");
                }
            }
            UpdateExcelFile();
        }

        // 更新 Excel.cs 文件
        static void UpdateExcelFile()
        {
            string[] fileNames = Directory.GetFiles(assetFolder, "*.asset").Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
            string variable = "";
            string loadCmd = "";
            foreach (var name in fileNames)
            {
                variable += string.Format("    public static {0} {0};\n", name);
                loadCmd += string.Format("        {0} = Resources.Load<{0}>(\"Excel/{0}\");\n", name);
            }
            variable = variable.Substring(0, variable.Length - 1);  //去除最后的换行
            loadCmd = loadCmd.Substring(0, loadCmd.Length - 1);

            string content = File.ReadAllText(excelClassTemplate);
            content = content.Replace("$Variable", variable);
            content = content.Replace("$LoadCmd", loadCmd);
            File.WriteAllText(excelClassFile, content);
        }
    }
}

